<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.db.models.fields.related &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../../index.html" />
    <link rel="up" title="django.db.models" href="../../models.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-models-fields-related">
            
  <h1>Source code for django.db.models.fields.related</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">attrgetter</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="kn">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">checks</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connection</span><span class="p">,</span> <span class="n">connections</span><span class="p">,</span> <span class="n">router</span><span class="p">,</span> <span class="n">transaction</span>
<span class="kn">from</span> <span class="nn">django.db.backends</span> <span class="kn">import</span> <span class="n">utils</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">signals</span><span class="p">,</span> <span class="n">Q</span>
<span class="kn">from</span> <span class="nn">django.db.models.deletion</span> <span class="kn">import</span> <span class="n">SET_NULL</span><span class="p">,</span> <span class="n">SET_DEFAULT</span><span class="p">,</span> <span class="n">CASCADE</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields</span> <span class="kn">import</span> <span class="p">(</span><span class="n">AutoField</span><span class="p">,</span> <span class="n">Field</span><span class="p">,</span> <span class="n">IntegerField</span><span class="p">,</span>
    <span class="n">PositiveIntegerField</span><span class="p">,</span> <span class="n">PositiveSmallIntegerField</span><span class="p">,</span> <span class="n">FieldDoesNotExist</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.models.lookups</span> <span class="kn">import</span> <span class="n">IsNull</span>
<span class="kn">from</span> <span class="nn">django.db.models.related</span> <span class="kn">import</span> <span class="n">RelatedObject</span><span class="p">,</span> <span class="n">PathInfo</span>
<span class="kn">from</span> <span class="nn">django.db.models.query</span> <span class="kn">import</span> <span class="n">QuerySet</span>
<span class="kn">from</span> <span class="nn">django.db.models.sql.datastructures</span> <span class="kn">import</span> <span class="n">Col</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">smart_text</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RenameMethodsBase</span><span class="p">,</span> <span class="n">RemovedInDjango18Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="kn">import</span> <span class="n">curry</span><span class="p">,</span> <span class="n">cached_property</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">exceptions</span>
<span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span> <span class="o">=</span> <span class="s">&#39;self&#39;</span>


<span class="k">def</span> <span class="nf">add_lazy_relation</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">relation</span><span class="p">,</span> <span class="n">operation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Adds a lookup on ``cls`` when a related field is defined using a string,</span>
<span class="sd">    i.e.::</span>

<span class="sd">        class MyModel(Model):</span>
<span class="sd">            fk = ForeignKey(&quot;AnotherModel&quot;)</span>

<span class="sd">    This string can be:</span>

<span class="sd">        * RECURSIVE_RELATIONSHIP_CONSTANT (i.e. &quot;self&quot;) to indicate a recursive</span>
<span class="sd">          relation.</span>

<span class="sd">        * The name of a model (i.e &quot;AnotherModel&quot;) to indicate another model in</span>
<span class="sd">          the same app.</span>

<span class="sd">        * An app-label and model name (i.e. &quot;someapp.AnotherModel&quot;) to indicate</span>
<span class="sd">          another model in a different app.</span>

<span class="sd">    If the other model hasn&#39;t yet been loaded -- almost a given if you&#39;re using</span>
<span class="sd">    lazy relationships -- then the relation won&#39;t be set up until the</span>
<span class="sd">    class_prepared signal fires at the end of model initialization.</span>

<span class="sd">    operation is the work that must be performed once the relation can be resolved.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># Check for recursive relations</span>
    <span class="k">if</span> <span class="n">relation</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">:</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span>
        <span class="n">model_name</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Look for an &quot;app.Model&quot; relation</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">relation</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="n">relation</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="c"># If we can&#39;t split, assume a model in current app</span>
                <span class="n">app_label</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span>
                <span class="n">model_name</span> <span class="o">=</span> <span class="n">relation</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># it&#39;s actually a model class</span>
            <span class="n">app_label</span> <span class="o">=</span> <span class="n">relation</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span>
            <span class="n">model_name</span> <span class="o">=</span> <span class="n">relation</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>

    <span class="c"># Try to look up the related model, and if it&#39;s already loaded resolve the</span>
    <span class="c"># string right away. If get_model returns None, it means that the related</span>
    <span class="c"># model isn&#39;t loaded yet, so we need to pend the relation until the class</span>
    <span class="c"># is prepared.</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_registered_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">LookupError</span><span class="p">:</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">operation</span><span class="p">)</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">_pending_lookups</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">operation</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">do_pending_lookups</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handle any pending relations to the sending model. Sent from class_prepared.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">sender</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">sender</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">cls</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">operation</span> <span class="ow">in</span> <span class="n">sender</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">_pending_lookups</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="p">[]):</span>
        <span class="n">operation</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>

<span class="n">signals</span><span class="o">.</span><span class="n">class_prepared</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">do_pending_lookups</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">RelatedField</span><span class="p">(</span><span class="n">Field</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_relation_model_exists</span><span class="p">())</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_referencing_to_swapped_model</span><span class="p">())</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_clashes</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_relation_model_exists</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rel_is_missing</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">()</span>
        <span class="n">rel_is_string</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span>
        <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="k">if</span> <span class="n">rel_is_string</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
        <span class="k">if</span> <span class="n">rel_is_missing</span> <span class="ow">and</span> <span class="p">(</span><span class="n">rel_is_string</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="p">(</span><span class="s">&quot;Field defines a relation with model &#39;</span><span class="si">%s</span><span class="s">&#39;, which &quot;</span>
                     <span class="s">&quot;is either not installed, or is abstract.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="n">model_name</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E300&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_referencing_to_swapped_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">()</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">):</span>
            <span class="n">model</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="p">(</span><span class="s">&quot;Field defines a relation with the model &#39;</span><span class="si">%s</span><span class="s">&#39;, &quot;</span>
                     <span class="s">&quot;which has been swapped out.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="n">model</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s">&quot;Update the relation to point at &#39;settings.</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E301&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_clashes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Check accessor and reverse query name clashes. &quot;&quot;&quot;</span>

        <span class="kn">from</span> <span class="nn">django.db.models.base</span> <span class="kn">import</span> <span class="n">ModelBase</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

        <span class="c"># `f.rel.to` may be a string instead of a model. Skip if model name is</span>
        <span class="c"># not resolved.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="c"># If the field doesn&#39;t install backward relation on the target model (so</span>
        <span class="c"># `is_hidden` returns True), then there are no clashes to check and we</span>
        <span class="c"># can skip these fields.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">():</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="c"># Consider that we are checking field `Model.foreign` and the models</span>
        <span class="c"># are:</span>
        <span class="c">#</span>
        <span class="c">#     class Target(models.Model):</span>
        <span class="c">#         model = models.IntegerField()</span>
        <span class="c">#         model_set = models.IntegerField()</span>
        <span class="c">#</span>
        <span class="c">#     class Model(models.Model):</span>
        <span class="c">#         foreign = models.ForeignKey(Target)</span>
        <span class="c">#         m2m = models.ManyToManyField(Target)</span>

        <span class="n">rel_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="c"># rel_opts.object_name == &quot;Target&quot;</span>
        <span class="n">rel_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span>  <span class="c"># i. e. &quot;model_set&quot;</span>
        <span class="n">rel_query_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">()</span>  <span class="c"># i. e. &quot;model&quot;</span>
        <span class="n">field_name</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>  <span class="c"># i. e. &quot;Model.field&quot;</span>

        <span class="c"># Check clashes between accessor or reverse query name of `field`</span>
        <span class="c"># and any other field name -- i. e. accessor for Model.foreign is</span>
        <span class="c"># model_set and it clashes with Target.model_set.</span>
        <span class="n">potential_clashes</span> <span class="o">=</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">fields</span> <span class="o">+</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">many_to_many</span>
        <span class="k">for</span> <span class="n">clash_field</span> <span class="ow">in</span> <span class="n">potential_clashes</span><span class="p">:</span>
            <span class="n">clash_name</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">rel_opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>  <span class="c"># i. e. &quot;Target.model_set&quot;</span>
            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">rel_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s">&quot;Reverse accessor for &#39;</span><span class="si">%s</span><span class="s">&#39; clashes with field name &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Rename field &#39;</span><span class="si">%s</span><span class="s">&#39;, or add/change a related_name &quot;</span>
                              <span class="s">&quot;argument to the definition for field &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">clash_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E302&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">rel_query_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s">&quot;Reverse query name for &#39;</span><span class="si">%s</span><span class="s">&#39; clashes with field name &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Rename field &#39;</span><span class="si">%s</span><span class="s">&#39;, or add/change a related_name &quot;</span>
                              <span class="s">&quot;argument to the definition for field &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">clash_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E303&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="c"># Check clashes between accessors/reverse query names of `field` and</span>
        <span class="c"># any other field accessor -- i. e. Model.foreign accessor clashes with</span>
        <span class="c"># Model.m2m accessor.</span>
        <span class="n">potential_clashes</span> <span class="o">=</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">get_all_related_many_to_many_objects</span><span class="p">()</span>
        <span class="n">potential_clashes</span> <span class="o">+=</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">get_all_related_objects</span><span class="p">()</span>
        <span class="n">potential_clashes</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">potential_clashes</span>
            <span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">field</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">clash_field</span> <span class="ow">in</span> <span class="n">potential_clashes</span><span class="p">:</span>
            <span class="n">clash_name</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>  <span class="c"># i. e. &quot;Model.m2m&quot;</span>
                <span class="n">clash_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="n">clash_field</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span> <span class="o">==</span> <span class="n">rel_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s">&quot;Reverse accessor for &#39;</span><span class="si">%s</span><span class="s">&#39; clashes with reverse accessor for &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Add or change a related_name argument &quot;</span>
                              <span class="s">&quot;to the definition for &#39;</span><span class="si">%s</span><span class="s">&#39; or &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E304&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span> <span class="o">==</span> <span class="n">rel_query_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s">&quot;Reverse query name for &#39;</span><span class="si">%s</span><span class="s">&#39; clashes with reverse query name for &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Add or change a related_name argument &quot;</span>
                              <span class="s">&quot;to the definition for &#39;</span><span class="si">%s</span><span class="s">&#39; or &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E305&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;By default related field will not have a column</span>
<span class="sd">           as it relates columns to another table&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">virtual_only</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="n">sup</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>

        <span class="c"># Store the opts for related_query_name()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">sup</span><span class="p">,</span> <span class="s">&#39;contribute_to_class&#39;</span><span class="p">):</span>
            <span class="n">sup</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">virtual_only</span><span class="o">=</span><span class="n">virtual_only</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span><span class="p">:</span>
            <span class="n">related_name</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
                <span class="s">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="n">related_name</span>
        <span class="n">other</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">resolve_related_class</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">cls</span><span class="p">):</span>
                <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">=</span> <span class="n">model</span>
                <span class="n">field</span><span class="o">.</span><span class="n">do_related_class</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>
            <span class="n">add_lazy_relation</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">resolve_related_class</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">do_related_class</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">swappable_setting</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the setting that this is powered from for swapping, or None</span>
<span class="sd">        if it&#39;s not swapped in / marked with swappable=False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span><span class="p">:</span>
            <span class="c"># Work out string form of &quot;to&quot;</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="n">to_string</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">to_string</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="c"># See if anything swapped/swappable matches</span>
            <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_swapped</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span> <span class="o">==</span> <span class="n">to_string</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span>
                <span class="k">if</span> <span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">))</span> <span class="o">==</span> <span class="n">to_string</span> <span class="ow">and</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">set_attributes_from_rel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span> <span class="o">+</span> <span class="s">&#39;_&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">set_field_name</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">do_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">cls</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_attributes_from_rel</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">RelatedObject</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">contribute_to_related_class</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_limit_choices_to</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns &#39;limit_choices_to&#39; for this model field.</span>

<span class="sd">        If it is a callable, it will be invoked and the result will be</span>
<span class="sd">        returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Passes ``limit_choices_to`` to field being constructed.</span>

<span class="sd">        Only passes it if there is a type that supports related fields.</span>
<span class="sd">        This is a similar strategy used to pass the ``queryset`` to the field</span>
<span class="sd">        being constructed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;get_related_field&#39;</span><span class="p">):</span>
            <span class="c"># If this is a callable, do not invoke it here. Just pass</span>
            <span class="c"># it in the defaults for when the form class will later be</span>
            <span class="c"># instantiated.</span>
            <span class="n">limit_choices_to</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span>
            <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
                <span class="s">&#39;limit_choices_to&#39;</span><span class="p">:</span> <span class="n">limit_choices_to</span><span class="p">,</span>
            <span class="p">})</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">related_query_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># This method defines the name that can be used to identify this</span>
        <span class="c"># related object in a table-spanning query. It uses the lower-cased</span>
        <span class="c"># object_name by default, but this can be overridden with the</span>
        <span class="c"># &quot;related_name&quot; option.</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_query_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">model_name</span>


<span class="k">class</span> <span class="nc">RenameRelatedObjectDescriptorMethods</span><span class="p">(</span><span class="n">RenameMethodsBase</span><span class="p">):</span>
    <span class="n">renamed_methods</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s">&#39;get_query_set&#39;</span><span class="p">,</span> <span class="s">&#39;get_queryset&#39;</span><span class="p">,</span> <span class="n">RemovedInDjango18Warning</span><span class="p">),</span>
        <span class="p">(</span><span class="s">&#39;get_prefetch_query_set&#39;</span><span class="p">,</span> <span class="s">&#39;get_prefetch_queryset&#39;</span><span class="p">,</span> <span class="n">RemovedInDjango18Warning</span><span class="p">),</span>
    <span class="p">)</span>


<span class="k">class</span> <span class="nc">SingleRelatedObjectDescriptor</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">RenameRelatedObjectDescriptorMethods</span><span class="p">)):</span>
    <span class="c"># This class provides the functionality that makes the related-object</span>
    <span class="c"># managers available as attributes on a model class, for fields that have</span>
    <span class="c"># a single &quot;remote&quot; value, on the class pointed to by a related field.</span>
    <span class="c"># In the example &quot;place.restaurant&quot;, the restaurant attribute is a</span>
    <span class="c"># SingleRelatedObjectDescriptor instance.</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">related</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span> <span class="o">=</span> <span class="n">related</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">RelatedObjectDoesNotExist</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># The exception isn&#39;t created at initialization time for the sake of</span>
        <span class="c"># consistency with `ReverseSingleRelatedObjectDescriptor`.</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span>
            <span class="nb">str</span><span class="p">(</span><span class="s">&#39;RelatedObjectDoesNotExist&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">,</span> <span class="ne">AttributeError</span><span class="p">),</span>
            <span class="p">{}</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_cached</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="c"># Gotcha: we return a `Manager` instance (i.e. not a `QuerySet`)!</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">hints</span><span class="o">=</span><span class="n">hints</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_prefetch_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># Despite its name `get_queryset()` returns an instance of</span>
            <span class="c"># `Manager`, therefore we call `all()` to normalize to `QuerySet`.</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="n">queryset</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instances</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="n">rel_obj_attr</span> <span class="o">=</span> <span class="n">attrgetter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="n">instance_attr</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">obj</span><span class="p">:</span> <span class="n">obj</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span>
        <span class="n">instances_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">instance_attr</span><span class="p">(</span><span class="n">inst</span><span class="p">),</span> <span class="n">inst</span><span class="p">)</span> <span class="k">for</span> <span class="n">inst</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">)</span>
        <span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">instances</span><span class="p">}</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">query</span><span class="p">)</span>

        <span class="c"># Since we&#39;re going to assign directly in the cache,</span>
        <span class="c"># we must manage the reverse relation cache manually.</span>
        <span class="n">rel_obj_cache_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">rel_obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
            <span class="n">instance</span> <span class="o">=</span> <span class="n">instances_dict</span><span class="p">[</span><span class="n">rel_obj_attr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">)]</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="n">rel_obj_cache_name</span><span class="p">,</span> <span class="n">instance</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">rel_obj_attr</span><span class="p">,</span> <span class="n">instance_attr</span><span class="p">,</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">rel_obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">related_pk</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">related_pk</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">rel_obj</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">params</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">for</span> <span class="n">lh_field</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_fields</span><span class="p">:</span>
                    <span class="n">params</span><span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">name</span><span class="p">)]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">rel_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="n">params</span><span class="p">)</span>
                <span class="k">except</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
                    <span class="n">rel_obj</span> <span class="o">=</span> <span class="bp">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">(),</span> <span class="n">instance</span><span class="p">)</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">,</span> <span class="n">rel_obj</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rel_obj</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="bp">self</span><span class="o">.</span><span class="n">RelatedObjectDoesNotExist</span><span class="p">(</span>
                <span class="s">&quot;</span><span class="si">%s</span><span class="s"> has no </span><span class="si">%s</span><span class="s">.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">rel_obj</span>

    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c"># The similarity of the code below to the code in</span>
        <span class="c"># ReverseSingleRelatedObjectDescriptor is annoying, but there&#39;s a bunch</span>
        <span class="c"># of small differences that would make a common base class convoluted.</span>

        <span class="c"># If null=True, we can assign null here, but otherwise the value needs</span>
        <span class="c"># to be an instance of the related class.</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">null</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s">&#39;Cannot assign None: &quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot; does not allow null values.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s">&#39;Cannot assign &quot;</span><span class="si">%r</span><span class="s">&quot;: &quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot; must be a &quot;</span><span class="si">%s</span><span class="s">&quot; instance.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">value</span><span class="p">,</span>
                    <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">value</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_relation</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;Cannot assign &quot;</span><span class="si">%r</span><span class="s">&quot;: the current database router prevents this relation.&#39;</span> <span class="o">%</span> <span class="n">value</span><span class="p">)</span>

        <span class="n">related_pk</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">None</span> <span class="ow">in</span> <span class="n">related_pk</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s">&#39;Cannot assign &quot;</span><span class="si">%r</span><span class="s">&quot;: &quot;</span><span class="si">%s</span><span class="s">&quot; instance isn</span><span class="se">\&#39;</span><span class="s">t saved in the database.&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="c"># Set the value of the related field to the value of the related object&#39;s related field</span>
        <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">):</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">related_pk</span><span class="p">[</span><span class="n">index</span><span class="p">])</span>

        <span class="c"># Since we already know what the related object is, seed the related</span>
        <span class="c"># object caches now, too. This avoids another db hit if you get the</span>
        <span class="c"># object you just set.</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">(),</span> <span class="n">instance</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ReverseSingleRelatedObjectDescriptor</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">RenameRelatedObjectDescriptorMethods</span><span class="p">)):</span>
    <span class="c"># This class provides the functionality that makes the related-object</span>
    <span class="c"># managers available as attributes on a model class, for fields that have</span>
    <span class="c"># a single &quot;remote&quot; value, on the class that defines the related field.</span>
    <span class="c"># In the example &quot;choice.poll&quot;, the poll attribute is a</span>
    <span class="c"># ReverseSingleRelatedObjectDescriptor instance.</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_with_rel</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">field_with_rel</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">RelatedObjectDoesNotExist</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># The exception can&#39;t be created at initialization time since the</span>
        <span class="c"># related model might not be resolved yet; `rel.to` might still be</span>
        <span class="c"># a string model reference.</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span>
            <span class="nb">str</span><span class="p">(</span><span class="s">&#39;RelatedObjectDoesNotExist&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">,</span> <span class="ne">AttributeError</span><span class="p">),</span>
            <span class="p">{}</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_cached</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="n">rel_mgr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">hints</span><span class="o">=</span><span class="n">hints</span><span class="p">)</span>
        <span class="c"># If the related manager indicates that it should be used for</span>
        <span class="c"># related fields, respect that.</span>
        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">rel_mgr</span><span class="p">,</span> <span class="s">&#39;use_for_related_fields&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">):</span>
            <span class="c"># Gotcha: we return a `Manager` instance (i.e. not a `QuerySet`)!</span>
            <span class="k">return</span> <span class="n">rel_mgr</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">QuerySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="n">hints</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_prefetch_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># Despite its name `get_queryset()` may return an instance of</span>
            <span class="c"># `Manager`, therefore we call `all()` to normalize to `QuerySet`.</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="n">queryset</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instances</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="n">rel_obj_attr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_foreign_related_value</span>
        <span class="n">instance_attr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_local_related_value</span>
        <span class="n">instances_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">instance_attr</span><span class="p">(</span><span class="n">inst</span><span class="p">),</span> <span class="n">inst</span><span class="p">)</span> <span class="k">for</span> <span class="n">inst</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">)</span>
        <span class="n">related_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c"># FIXME: This will need to be revisited when we introduce support for</span>
        <span class="c"># composite fields. In the meantime we take this practical approach to</span>
        <span class="c"># solve a regression on 1.6 when the reverse manager in hidden</span>
        <span class="c"># (related_name ends with a &#39;+&#39;). Refs #21410.</span>
        <span class="c"># The check for len(...) == 1 is a special case that allows the query</span>
        <span class="c"># to be join-less and smaller. Refs #21760.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="n">related_field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="nb">set</span><span class="p">(</span><span class="n">instance_attr</span><span class="p">(</span><span class="n">inst</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">inst</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">)}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">():</span> <span class="n">instances</span><span class="p">}</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">query</span><span class="p">)</span>

        <span class="c"># Since we&#39;re going to assign directly in the cache,</span>
        <span class="c"># we must manage the reverse relation cache manually.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">multiple</span><span class="p">:</span>
            <span class="n">rel_obj_cache_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">rel_obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
                <span class="n">instance</span> <span class="o">=</span> <span class="n">instances_dict</span><span class="p">[</span><span class="n">rel_obj_attr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">)]</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="n">rel_obj_cache_name</span><span class="p">,</span> <span class="n">instance</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">rel_obj_attr</span><span class="p">,</span> <span class="n">instance_attr</span><span class="p">,</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">rel_obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_local_related_value</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">None</span> <span class="ow">in</span> <span class="n">val</span><span class="p">:</span>
                <span class="n">rel_obj</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">params</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
                    <span class="p">(</span><span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">lh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
                    <span class="k">for</span> <span class="n">lh_field</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_fields</span><span class="p">)</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">)</span>
                <span class="n">extra_filter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_extra_descriptor_filter</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">extra_filter</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                    <span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_filter</span><span class="p">)</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">params</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">extra_filter</span><span class="p">,</span> <span class="o">**</span><span class="n">params</span><span class="p">)</span>
                <span class="c"># Assuming the database enforces foreign keys, this won&#39;t fail.</span>
                <span class="n">rel_obj</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">multiple</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">(),</span> <span class="n">instance</span><span class="p">)</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">,</span> <span class="n">rel_obj</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rel_obj</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="k">raise</span> <span class="bp">self</span><span class="o">.</span><span class="n">RelatedObjectDoesNotExist</span><span class="p">(</span>
                <span class="s">&quot;</span><span class="si">%s</span><span class="s"> has no </span><span class="si">%s</span><span class="s">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">rel_obj</span>

    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c"># If null=True, we can assign null here, but otherwise the value needs</span>
        <span class="c"># to be an instance of the related class.</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">null</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s">&#39;Cannot assign None: &quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot; does not allow null values.&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s">&#39;Cannot assign &quot;</span><span class="si">%r</span><span class="s">&quot;: &quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot; must be a &quot;</span><span class="si">%s</span><span class="s">&quot; instance.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">value</span><span class="p">,</span>
                    <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">value</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">value</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_relation</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;Cannot assign &quot;</span><span class="si">%r</span><span class="s">&quot;: the current database router prevents this relation.&#39;</span> <span class="o">%</span> <span class="n">value</span><span class="p">)</span>

        <span class="c"># If we&#39;re setting the value of a OneToOneField to None, we need to clear</span>
        <span class="c"># out the cache on any old related object. Otherwise, deleting the</span>
        <span class="c"># previously-related object will also cause this object to be deleted,</span>
        <span class="c"># which is wrong.</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># Look up the previously-related object, which may still be available</span>
            <span class="c"># since we&#39;ve not yet cleared out the related field.</span>
            <span class="c"># Use the cache directly, instead of the accessor; if we haven&#39;t</span>
            <span class="c"># populated the cache, then we don&#39;t care - we&#39;re only accessing</span>
            <span class="c"># the object to invalidate the accessor cache, so there&#39;s no</span>
            <span class="c"># need to populate the cache just to expire it again.</span>
            <span class="n">related</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

            <span class="c"># If we&#39;ve got an old related object, we need to clear out its</span>
            <span class="c"># cache. This cache also might not exist if the related object</span>
            <span class="c"># hasn&#39;t been accessed yet.</span>
            <span class="k">if</span> <span class="n">related</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">related</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">(),</span> <span class="bp">None</span><span class="p">)</span>

        <span class="c"># Set the value of the related field</span>
        <span class="k">for</span> <span class="n">lh_field</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_fields</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">lh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">lh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

        <span class="c"># Since we already know what the related object is, seed the related</span>
        <span class="c"># object caches now, too. This avoids another db hit if you get the</span>
        <span class="c"># object you just set.</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">multiple</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">get_cache_name</span><span class="p">(),</span> <span class="n">instance</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">create_foreign_related_manager</span><span class="p">(</span><span class="n">superclass</span><span class="p">,</span> <span class="n">rel_field</span><span class="p">,</span> <span class="n">rel_model</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">RelatedManager</span><span class="p">(</span><span class="n">superclass</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">instance</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">core_filters</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__exact&#39;</span> <span class="o">%</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">instance</span><span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">rel_model</span>

        <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c"># We use **kwargs rather than a kwarg argument to enforce the</span>
            <span class="c"># `manager=&#39;manager_name&#39;` syntax.</span>
            <span class="n">manager</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;manager&#39;</span><span class="p">))</span>
            <span class="n">manager_class</span> <span class="o">=</span> <span class="n">create_foreign_related_manager</span><span class="p">(</span><span class="n">manager</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">rel_field</span><span class="p">,</span> <span class="n">rel_model</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">manager_class</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
        <span class="n">do_not_call_in_templates</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span><span class="p">[</span><span class="n">rel_field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">()]</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
                <span class="n">db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="n">empty_strings_as_null</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
                <span class="n">qs</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">:</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">core_filters</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">:</span>
                    <span class="n">val</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="p">(</span><span class="n">val</span> <span class="o">==</span> <span class="s">&#39;&#39;</span> <span class="ow">and</span> <span class="n">empty_strings_as_null</span><span class="p">):</span>
                        <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>
                <span class="n">qs</span><span class="o">.</span><span class="n">_known_related_objects</span> <span class="o">=</span> <span class="p">{</span><span class="n">rel_field</span><span class="p">:</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">}}</span>
                <span class="k">return</span> <span class="n">qs</span>

        <span class="k">def</span> <span class="nf">get_prefetch_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">queryset</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>

            <span class="n">queryset</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instances</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>

            <span class="n">rel_obj_attr</span> <span class="o">=</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">get_local_related_value</span>
            <span class="n">instance_attr</span> <span class="o">=</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">get_foreign_related_value</span>
            <span class="n">instances_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">instance_attr</span><span class="p">(</span><span class="n">inst</span><span class="p">),</span> <span class="n">inst</span><span class="p">)</span> <span class="k">for</span> <span class="n">inst</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">)</span>
            <span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">instances</span><span class="p">}</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">query</span><span class="p">)</span>

            <span class="c"># Since we just bypassed this class&#39; get_queryset(), we must manage</span>
            <span class="c"># the reverse relation manually.</span>
            <span class="k">for</span> <span class="n">rel_obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
                <span class="n">instance</span> <span class="o">=</span> <span class="n">instances_dict</span><span class="p">[</span><span class="n">rel_obj_attr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">)]</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">instance</span><span class="p">)</span>
            <span class="n">cache_name</span> <span class="o">=</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">rel_obj_attr</span><span class="p">,</span> <span class="n">instance_attr</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="n">cache_name</span>

        <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">):</span>
            <span class="n">objs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">objs</span><span class="p">)</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">commit_on_success_unless_managed</span><span class="p">(</span>
                    <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39; instance expected, got </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span>
                                        <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                    <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="n">add</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">get_or_create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">update_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">RelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">update_or_create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">update_or_create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="c"># remove() and clear() are only provided if the ForeignKey can have a value of null.</span>
        <span class="k">if</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">objs</span><span class="p">:</span>
                    <span class="k">return</span>
                <span class="n">bulk</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;bulk&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
                <span class="n">val</span> <span class="o">=</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">get_foreign_related_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="n">old_ids</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
                    <span class="c"># Is obj actually part of this descriptor set?</span>
                    <span class="k">if</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">get_local_related_value</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span> <span class="o">==</span> <span class="n">val</span><span class="p">:</span>
                        <span class="n">old_ids</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%r</span><span class="s"> is not related to </span><span class="si">%r</span><span class="s">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_clear</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="n">old_ids</span><span class="p">),</span> <span class="n">bulk</span><span class="p">)</span>
            <span class="n">remove</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

            <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="n">bulk</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;bulk&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_clear</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bulk</span><span class="p">)</span>
            <span class="n">clear</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

            <span class="k">def</span> <span class="nf">_clear</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">bulk</span><span class="p">):</span>
                <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">bulk</span><span class="p">:</span>
                    <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="bp">None</span><span class="p">})</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">commit_on_success_unless_managed</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
                        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
                            <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
                            <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">update_fields</span><span class="o">=</span><span class="p">[</span><span class="n">rel_field</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
            <span class="n">_clear</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">return</span> <span class="n">RelatedManager</span>


<span class="k">class</span> <span class="nc">ForeignRelatedObjectsDescriptor</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="c"># This class provides the functionality that makes the related-object</span>
    <span class="c"># managers available as attributes on a model class, for fields that have</span>
    <span class="c"># multiple &quot;remote&quot; values and have a ForeignKey pointed at them by</span>
    <span class="c"># some other model. In the example &quot;poll.choice_set&quot;, the choice_set</span>
    <span class="c"># attribute is a ForeignRelatedObjectsDescriptor instance.</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">related</span>   <span class="c"># RelatedObject instance</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_manager_cls</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">manager</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
        <span class="c"># If the foreign key can support nulls, then completely clear the related set.</span>
        <span class="c"># Otherwise, just move the named objects into the set.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="n">manager</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="o">*</span><span class="n">value</span><span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">related_manager_cls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Dynamically create a class that subclasses the related model&#39;s default</span>
        <span class="c"># manager.</span>
        <span class="k">return</span> <span class="n">create_foreign_related_manager</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
        <span class="p">)</span>


<span class="k">def</span> <span class="nf">create_many_related_manager</span><span class="p">(</span><span class="n">superclass</span><span class="p">,</span> <span class="n">rel</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Creates a manager that subclasses &#39;superclass&#39; (which is a Manager)</span>
<span class="sd">    and adds behavior for many-to-many related objects.&quot;&quot;&quot;</span>
    <span class="k">class</span> <span class="nc">ManyRelatedManager</span><span class="p">(</span><span class="n">superclass</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">query_field_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">symmetrical</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                     <span class="n">source_field_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">target_field_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                     <span class="n">through</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">prefetch_cache_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">query_field_name</span> <span class="o">=</span> <span class="n">query_field_name</span>

            <span class="n">source_field</span> <span class="o">=</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">source_field_name</span><span class="p">)</span>
            <span class="n">source_related_fields</span> <span class="o">=</span> <span class="n">source_field</span><span class="o">.</span><span class="n">related_fields</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">core_filters</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">lh_field</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="n">source_related_fields</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">core_filters</span><span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">query_field_name</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">name</span><span class="p">)]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">instance</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">symmetrical</span> <span class="o">=</span> <span class="n">symmetrical</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">source_field</span> <span class="o">=</span> <span class="n">source_field</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span> <span class="o">=</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">target_field_name</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span> <span class="o">=</span> <span class="n">source_field_name</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span> <span class="o">=</span> <span class="n">target_field_name</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reverse</span> <span class="o">=</span> <span class="n">reverse</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">through</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_cache_name</span> <span class="o">=</span> <span class="n">prefetch_cache_name</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span> <span class="o">=</span> <span class="n">source_field</span><span class="o">.</span><span class="n">get_foreign_related_value</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">None</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;&quot;</span><span class="si">%r</span><span class="s">&quot; needs to have a value for field &quot;</span><span class="si">%s</span><span class="s">&quot; before &#39;</span>
                                 <span class="s">&#39;this many-to-many relationship can be used.&#39;</span> <span class="o">%</span>
                                 <span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">source_field_name</span><span class="p">))</span>
            <span class="c"># Even if this relation is not to pk, we require still pk value.</span>
            <span class="c"># The wish is that the instance has been already saved to DB,</span>
            <span class="c"># although having a pk value isn&#39;t a guarantee of that.</span>
            <span class="k">if</span> <span class="n">instance</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%r</span><span class="s"> instance needs to have a primary key value before &quot;</span>
                                 <span class="s">&quot;a many-to-many relationship can be used.&quot;</span> <span class="o">%</span>
                                 <span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c"># We use **kwargs rather than a kwarg argument to enforce the</span>
            <span class="c"># `manager=&#39;manager_name&#39;` syntax.</span>
            <span class="n">manager</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;manager&#39;</span><span class="p">))</span>
            <span class="n">manager_class</span> <span class="o">=</span> <span class="n">create_many_related_manager</span><span class="p">(</span><span class="n">manager</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">rel</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">manager_class</span><span class="p">(</span>
                <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
                <span class="n">query_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">query_field_name</span><span class="p">,</span>
                <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span>
                <span class="n">symmetrical</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">symmetrical</span><span class="p">,</span>
                <span class="n">source_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">,</span>
                <span class="n">target_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">,</span>
                <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                <span class="n">through</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                <span class="n">prefetch_cache_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">prefetch_cache_name</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="n">do_not_call_in_templates</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_build_remove_filters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">removed_vals</span><span class="p">):</span>
            <span class="n">filters</span> <span class="o">=</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span><span class="p">})</span>
            <span class="c"># No need to add a subquery condition if removed_vals is a QuerySet without</span>
            <span class="c"># filters.</span>
            <span class="n">removed_vals_filters</span> <span class="o">=</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">removed_vals</span><span class="p">,</span> <span class="n">QuerySet</span><span class="p">)</span> <span class="ow">or</span>
                                    <span class="n">removed_vals</span><span class="o">.</span><span class="n">_has_filters</span><span class="p">())</span>
            <span class="k">if</span> <span class="n">removed_vals_filters</span><span class="p">:</span>
                <span class="n">filters</span> <span class="o">&amp;=</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">:</span> <span class="n">removed_vals</span><span class="p">})</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">symmetrical</span><span class="p">:</span>
                <span class="n">symmetrical_filters</span> <span class="o">=</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span><span class="p">})</span>
                <span class="k">if</span> <span class="n">removed_vals_filters</span><span class="p">:</span>
                    <span class="n">symmetrical_filters</span> <span class="o">&amp;=</span> <span class="n">Q</span><span class="p">(</span>
                        <span class="o">**</span><span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">:</span> <span class="n">removed_vals</span><span class="p">})</span>
                <span class="n">filters</span> <span class="o">|=</span> <span class="n">symmetrical_filters</span>
            <span class="k">return</span> <span class="n">filters</span>

        <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefetch_cache_name</span><span class="p">]</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
                <span class="n">qs</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">:</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">_next_is_sticky</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">core_filters</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">get_prefetch_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instances</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">queryset</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>

            <span class="n">queryset</span><span class="o">.</span><span class="n">_add_hints</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">instances</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>

            <span class="n">query</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">query_field_name</span><span class="p">:</span> <span class="n">instances</span><span class="p">}</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_next_is_sticky</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">query</span><span class="p">)</span>

            <span class="c"># M2M: need to annotate the query in order to get the primary model</span>
            <span class="c"># that the secondary model was actually related to. We know that</span>
            <span class="c"># there will already be a join on the join table, so we can just add</span>
            <span class="c"># the select.</span>

            <span class="c"># For non-autocreated &#39;through&#39; models, can&#39;t assume we are</span>
            <span class="c"># dealing with PK values.</span>
            <span class="n">fk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">)</span>
            <span class="n">join_table</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
            <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">queryset</span><span class="o">.</span><span class="n">db</span><span class="p">]</span>
            <span class="n">qn</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">quote_name</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">extra</span><span class="p">(</span><span class="n">select</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span>
                <span class="p">(</span><span class="s">&#39;_prefetch_related_val_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span>
                <span class="s">&#39;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">qn</span><span class="p">(</span><span class="n">join_table</span><span class="p">),</span> <span class="n">qn</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">column</span><span class="p">)))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fk</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">))</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">queryset</span><span class="p">,</span>
                    <span class="k">lambda</span> <span class="n">result</span><span class="p">:</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="s">&#39;_prefetch_related_val_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fk</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">),</span>
                    <span class="k">lambda</span> <span class="n">inst</span><span class="p">:</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fk</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">),</span>
                    <span class="bp">False</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_cache_name</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span>
                    <span class="s">&quot;Cannot use add() on a ManyToManyField which specifies an intermediary model. Use </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;s Manager instead.&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_add_items</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">)</span>

            <span class="c"># If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">symmetrical</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_add_items</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">)</span>
        <span class="n">add</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span>
                    <span class="s">&quot;Cannot use remove() on a ManyToManyField which specifies an intermediary model. Use </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;s Manager instead.&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_remove_items</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field_name</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">)</span>
        <span class="n">remove</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>

            <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;pre_clear&quot;</span><span class="p">,</span>
                <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>

            <span class="n">filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_remove_filters</span><span class="p">(</span><span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">filters</span><span class="p">)</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>

            <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;post_clear&quot;</span><span class="p">,</span>
                <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
        <span class="n">clear</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="c"># This check needs to be done here, since we can&#39;t later remove this</span>
            <span class="c"># from the method lookup table, as we do with add and remove.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span>
                    <span class="s">&quot;Cannot use create() on a ManyToManyField which specifies an intermediary model. Use </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;s Manager instead.&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="n">new_obj</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">new_obj</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">new_obj</span>
        <span class="n">create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="n">obj</span><span class="p">,</span> <span class="n">created</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="c"># We only need to add() if created because if we got an object back</span>
            <span class="c"># from get() then the relationship already exists.</span>
            <span class="k">if</span> <span class="n">created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">obj</span><span class="p">,</span> <span class="n">created</span>
        <span class="n">get_or_create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">update_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
            <span class="n">obj</span><span class="p">,</span> <span class="n">created</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_manager</span><span class="p">(</span><span class="n">db</span><span class="p">))</span><span class="o">.</span><span class="n">update_or_create</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="c"># We only need to add() if created because if we got an object back</span>
            <span class="c"># from get() then the relationship already exists.</span>
            <span class="k">if</span> <span class="n">created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">obj</span><span class="p">,</span> <span class="n">created</span>
        <span class="n">update_or_create</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="k">def</span> <span class="nf">_add_items</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_field_name</span><span class="p">,</span> <span class="n">target_field_name</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">):</span>
            <span class="c"># source_field_name: the PK fieldname in join table for the source object</span>
            <span class="c"># target_field_name: the PK fieldname in join table for the target object</span>
            <span class="c"># *objs - objects to add. Either object instances, or primary keys of object instances.</span>

            <span class="c"># If there aren&#39;t any objects, there is nothing to do.</span>
            <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Model</span>
            <span class="k">if</span> <span class="n">objs</span><span class="p">:</span>
                <span class="n">new_ids</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">router</span><span class="o">.</span><span class="n">allow_relation</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">):</span>
                            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                                <span class="s">&#39;Cannot add &quot;</span><span class="si">%r</span><span class="s">&quot;: instance is on database &quot;</span><span class="si">%s</span><span class="s">&quot;, value is on database &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span>
                                <span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
                            <span class="p">)</span>
                        <span class="n">fk_val</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span>
                            <span class="n">target_field_name</span><span class="p">)</span><span class="o">.</span><span class="n">get_foreign_related_value</span><span class="p">(</span><span class="n">obj</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="k">if</span> <span class="n">fk_val</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                                <span class="s">&#39;Cannot add &quot;</span><span class="si">%r</span><span class="s">&quot;: the value for field &quot;</span><span class="si">%s</span><span class="s">&quot; is None&#39;</span> <span class="o">%</span>
                                <span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">target_field_name</span><span class="p">)</span>
                            <span class="p">)</span>
                        <span class="n">new_ids</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fk_val</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">Model</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                            <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39; instance expected, got </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">new_ids</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
                <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>
                <span class="n">vals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">values_list</span><span class="p">(</span><span class="n">target_field_name</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
                <span class="n">vals</span> <span class="o">=</span> <span class="n">vals</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                    <span class="n">source_field_name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                    <span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="n">target_field_name</span><span class="p">:</span> <span class="n">new_ids</span><span class="p">,</span>
                <span class="p">})</span>
                <span class="n">new_ids</span> <span class="o">=</span> <span class="n">new_ids</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">vals</span><span class="p">)</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse</span> <span class="ow">or</span> <span class="n">source_field_name</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">:</span>
                    <span class="c"># Don&#39;t send the signal when we are inserting the</span>
                    <span class="c"># duplicate data row for symmetrical reverse entries.</span>
                    <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;pre_add&#39;</span><span class="p">,</span>
                        <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                        <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="n">new_ids</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
                <span class="c"># Add the ones that aren&#39;t there already</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">bulk_create</span><span class="p">([</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                        <span class="s">&#39;</span><span class="si">%s</span><span class="s">_id&#39;</span> <span class="o">%</span> <span class="n">source_field_name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_val</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                        <span class="s">&#39;</span><span class="si">%s</span><span class="s">_id&#39;</span> <span class="o">%</span> <span class="n">target_field_name</span><span class="p">:</span> <span class="n">obj_id</span><span class="p">,</span>
                    <span class="p">})</span>
                    <span class="k">for</span> <span class="n">obj_id</span> <span class="ow">in</span> <span class="n">new_ids</span>
                <span class="p">])</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse</span> <span class="ow">or</span> <span class="n">source_field_name</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">source_field_name</span><span class="p">:</span>
                    <span class="c"># Don&#39;t send the signal when we are inserting the</span>
                    <span class="c"># duplicate data row for symmetrical reverse entries.</span>
                    <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;post_add&#39;</span><span class="p">,</span>
                        <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                        <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="n">new_ids</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_remove_items</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_field_name</span><span class="p">,</span> <span class="n">target_field_name</span><span class="p">,</span> <span class="o">*</span><span class="n">objs</span><span class="p">):</span>
            <span class="c"># source_field_name: the PK colname in join table for the source object</span>
            <span class="c"># target_field_name: the PK colname in join table for the target object</span>
            <span class="c"># *objs - objects to remove</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">objs</span><span class="p">:</span>
                <span class="k">return</span>

            <span class="c"># Check that all the objects are of the right type</span>
            <span class="n">old_ids</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
                    <span class="n">fk_val</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">get_foreign_related_value</span><span class="p">(</span><span class="n">obj</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">old_ids</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fk_val</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">old_ids</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

            <span class="n">db</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">)</span>

            <span class="c"># Send a signal to the other end if need be.</span>
            <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;pre_remove&quot;</span><span class="p">,</span>
                <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="n">old_ids</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
            <span class="n">target_model_qs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyRelatedManager</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">target_model_qs</span><span class="o">.</span><span class="n">_has_filters</span><span class="p">():</span>
                <span class="n">old_vals</span> <span class="o">=</span> <span class="n">target_model_qs</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                    <span class="s">&#39;</span><span class="si">%s</span><span class="s">__in&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">attname</span><span class="p">:</span> <span class="n">old_ids</span><span class="p">})</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">old_vals</span> <span class="o">=</span> <span class="n">old_ids</span>
            <span class="n">filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_remove_filters</span><span class="p">(</span><span class="n">old_vals</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">filters</span><span class="p">)</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>

            <span class="n">signals</span><span class="o">.</span><span class="n">m2m_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;post_remove&quot;</span><span class="p">,</span>
                <span class="n">instance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">,</span>
                <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">pk_set</span><span class="o">=</span><span class="n">old_ids</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">ManyRelatedManager</span>


<span class="k">class</span> <span class="nc">ManyRelatedObjectsDescriptor</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="c"># This class provides the functionality that makes the related-object</span>
    <span class="c"># managers available as attributes on a model class, for fields that have</span>
    <span class="c"># multiple &quot;remote&quot; values and have a ManyToManyField pointed at them by</span>
    <span class="c"># some other model (rather than having a ManyToManyField themselves).</span>
    <span class="c"># In the example &quot;publication.article_set&quot;, the article_set attribute is a</span>
    <span class="c"># ManyRelatedObjectsDescriptor instance.</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">related</span>   <span class="c"># RelatedObject instance</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">related_manager_cls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Dynamically create a class that subclasses the related</span>
        <span class="c"># model&#39;s default manager.</span>
        <span class="k">return</span> <span class="n">create_many_related_manager</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>

        <span class="n">rel_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">model</span>

        <span class="n">manager</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_manager_cls</span><span class="p">(</span>
            <span class="n">model</span><span class="o">=</span><span class="n">rel_model</span><span class="p">,</span>
            <span class="n">query_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="n">prefetch_cache_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">(),</span>
            <span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">,</span>
            <span class="n">symmetrical</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">source_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">(),</span>
            <span class="n">target_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">(),</span>
            <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
            <span class="n">through</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="n">manager</span>

    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s">&quot;Cannot set values on a ManyToManyField which specifies an intermediary model. Use </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;s Manager instead.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">))</span>

        <span class="n">manager</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="o">*</span><span class="n">value</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ReverseManyRelatedObjectsDescriptor</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="c"># This class provides the functionality that makes the related-object</span>
    <span class="c"># managers available as attributes on a model class, for fields that have</span>
    <span class="c"># multiple &quot;remote&quot; values and have a ManyToManyField defined in their</span>
    <span class="c"># model (rather than having another model pointed *at* them).</span>
    <span class="c"># In the example &quot;article.publications&quot;, the publications attribute is a</span>
    <span class="c"># ReverseManyRelatedObjectsDescriptor instance.</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m2m_field</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">m2m_field</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">through</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># through is provided so that you have easy access to the through</span>
        <span class="c"># model (Book.authors.through) for inlines, etc. This is done as</span>
        <span class="c"># a property to ensure that the fully resolved value is returned.</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">related_manager_cls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Dynamically create a class that subclasses the related model&#39;s</span>
        <span class="c"># default manager.</span>
        <span class="k">return</span> <span class="n">create_many_related_manager</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>

        <span class="n">manager</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_manager_cls</span><span class="p">(</span>
            <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span>
            <span class="n">query_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">(),</span>
            <span class="n">prefetch_cache_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="n">instance</span><span class="o">=</span><span class="n">instance</span><span class="p">,</span>
            <span class="n">symmetrical</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">symmetrical</span><span class="p">,</span>
            <span class="n">source_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">(),</span>
            <span class="n">target_field_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">(),</span>
            <span class="n">reverse</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">through</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="n">manager</span>

    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s">&quot;Cannot set values on a ManyToManyField which specifies an intermediary model.  Use </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;s Manager instead.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">))</span>

        <span class="n">manager</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
        <span class="c"># clear() can change expected output of &#39;value&#39; queryset, we force evaluation</span>
        <span class="c"># of queryset before clear; ticket #19816</span>
        <span class="n">value</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="o">*</span><span class="n">value</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ForeignObjectRel</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">parent_link</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>  <span class="c"># to._meta doesn&#39;t exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT</span>
            <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">),</span> <span class="s">&quot;&#39;to&#39; must be either a model, a model name or the string </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">field</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to</span> <span class="o">=</span> <span class="n">to</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="n">related_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_query_name</span> <span class="o">=</span> <span class="n">related_query_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="n">limit_choices_to</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">limit_choices_to</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">multiple</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent_link</span> <span class="o">=</span> <span class="n">parent_link</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_delete</span> <span class="o">=</span> <span class="n">on_delete</span>

    <span class="k">def</span> <span class="nf">is_hidden</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Should the related object be hidden?&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;+&#39;</span>

    <span class="k">def</span> <span class="nf">get_joining_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_reverse_joining_columns</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_extra_restriction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">where_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">related_alias</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_extra_restriction</span><span class="p">(</span><span class="n">where_class</span><span class="p">,</span> <span class="n">related_alias</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_field_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Sets the related field&#39;s name, this is not available until later stages</span>
<span class="sd">        of app loading, so set_field_name is called from</span>
<span class="sd">        set_attributes_from_rel()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># By default foreign object doesn&#39;t relate to any remote field (for</span>
        <span class="c"># example custom multicolumn joins currently have no remote field).</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field_name</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_lookup_constraint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">constraint_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">lookup_type</span><span class="p">,</span>
                              <span class="n">raw_value</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">get_lookup_constraint</span><span class="p">(</span><span class="n">constraint_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span>
                                                <span class="n">lookup_type</span><span class="p">,</span> <span class="n">raw_value</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ManyToOneRel</span><span class="p">(</span><span class="n">ForeignObjectRel</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">parent_link</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ManyToOneRel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
            <span class="n">field</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="n">related_name</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">limit_choices_to</span><span class="p">,</span>
            <span class="n">parent_link</span><span class="o">=</span><span class="n">parent_link</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">on_delete</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="n">related_query_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field_name</span> <span class="o">=</span> <span class="n">field_name</span>

    <span class="k">def</span> <span class="nf">get_related_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the Field in the &#39;to&#39; object to which this relationship is</span>
<span class="sd">        tied.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">data</span><span class="p">[</span><span class="mi">2</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">FieldDoesNotExist</span><span class="p">(</span><span class="s">&quot;No related field named &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">set_field_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">field_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span>


<span class="k">class</span> <span class="nc">OneToOneRel</span><span class="p">(</span><span class="n">ManyToOneRel</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">parent_link</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">OneToOneRel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span>
                <span class="n">related_name</span><span class="o">=</span><span class="n">related_name</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">limit_choices_to</span><span class="p">,</span>
                <span class="n">parent_link</span><span class="o">=</span><span class="n">parent_link</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">on_delete</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="n">related_query_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">multiple</span> <span class="o">=</span> <span class="bp">False</span>


<span class="k">class</span> <span class="nc">ManyToManyRel</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">symmetrical</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">through_fields</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">db_constraint</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">through</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">db_constraint</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Can&#39;t supply a through model and db_constraint=False&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">through_fields</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">through</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot specify through_fields without a through model&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to</span> <span class="o">=</span> <span class="n">to</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="n">related_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_query_name</span> <span class="o">=</span> <span class="n">related_query_name</span>
        <span class="k">if</span> <span class="n">limit_choices_to</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">limit_choices_to</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">limit_choices_to</span> <span class="o">=</span> <span class="n">limit_choices_to</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symmetrical</span> <span class="o">=</span> <span class="n">symmetrical</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">multiple</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">through</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">through_fields</span> <span class="o">=</span> <span class="n">through_fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span> <span class="o">=</span> <span class="n">db_constraint</span>

    <span class="k">def</span> <span class="nf">is_hidden</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Should the related object be hidden?&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_name</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;+&#39;</span>

    <span class="k">def</span> <span class="nf">get_related_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the field in the to&#39; object to which this relationship is tied</span>
<span class="sd">        (this is always the primary key on the target model). Provided for</span>
<span class="sd">        symmetry with ManyToOneRel.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span>


<span class="k">class</span> <span class="nc">ForeignObject</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">):</span>
    <span class="n">requires_unique_target</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">generate_reverse_relation</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">related_accessor_class</span> <span class="o">=</span> <span class="n">ForeignRelatedObjectsDescriptor</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">from_fields</span><span class="p">,</span> <span class="n">to_fields</span><span class="p">,</span> <span class="n">swappable</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span> <span class="o">=</span> <span class="n">from_fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span> <span class="o">=</span> <span class="n">to_fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span> <span class="o">=</span> <span class="n">swappable</span>

        <span class="k">if</span> <span class="s">&#39;rel&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;rel&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ForeignObjectRel</span><span class="p">(</span>
                <span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span>
                <span class="n">related_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
                <span class="n">related_query_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_query_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
                <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;limit_choices_to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
                <span class="n">parent_link</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;parent_link&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">),</span>
                <span class="n">on_delete</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;on_delete&#39;</span><span class="p">,</span> <span class="n">CASCADE</span><span class="p">),</span>
            <span class="p">)</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;verbose_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;verbose_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_unique_target</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_unique_target</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rel_is_string</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rel_is_string</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">requires_unique_target</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="c"># Skip if the</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span>
        <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">related</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="n">has_unique_field</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span><span class="n">rel_field</span><span class="o">.</span><span class="n">unique</span>
            <span class="k">for</span> <span class="n">rel_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">has_unique_field</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">field_combination</span> <span class="o">=</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span>
                <span class="k">for</span> <span class="n">rel_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span>
            <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">__name__</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s">&quot;None of the fields </span><span class="si">%s</span><span class="s"> on model &#39;</span><span class="si">%s</span><span class="s">&#39; have a unique=True constraint.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_combination</span><span class="p">,</span> <span class="n">model_name</span><span class="p">),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E310&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">has_unique_field</span><span class="p">:</span>
            <span class="n">field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
            <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">__name__</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="p">(</span><span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39; must set unique=True &quot;</span>
                     <span class="s">&quot;because it is referenced by a foreign key.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E311&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;from_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;related_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_query_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;related_query_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_query_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">on_delete</span> <span class="o">!=</span> <span class="n">CASCADE</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;on_delete&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">on_delete</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;parent_link&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">parent_link</span>
        <span class="c"># Work out string form of &quot;to&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
        <span class="c"># If swappable is True, then see if we&#39;re actually pointing to the target</span>
        <span class="c"># of a swap.</span>
        <span class="n">swappable_setting</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable_setting</span>
        <span class="k">if</span> <span class="n">swappable_setting</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># If it&#39;s already a settings reference, error</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">],</span> <span class="s">&quot;setting_name&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span> <span class="o">!=</span> <span class="n">swappable_setting</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot deconstruct a ForeignKey pointing to a model that is swapped in place of more than one model (</span><span class="si">%s</span><span class="s"> and </span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span><span class="p">,</span> <span class="n">swappable_setting</span><span class="p">))</span>
            <span class="c"># Set it</span>
            <span class="kn">from</span> <span class="nn">django.db.migrations.writer</span> <span class="kn">import</span> <span class="n">SettingsReference</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">SettingsReference</span><span class="p">(</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">],</span>
                <span class="n">swappable_setting</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">resolve_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;Foreign Object from and to fields must be the same non-zero length&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;Related model </span><span class="si">%r</span><span class="s"> cannot be resolved&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">)</span>
        <span class="n">related_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">)):</span>
            <span class="n">from_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
            <span class="n">to_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
            <span class="n">from_field</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span> <span class="k">if</span> <span class="n">from_field_name</span> <span class="o">==</span> <span class="s">&#39;self&#39;</span>
                          <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">from_field_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="k">if</span> <span class="n">to_field_name</span> <span class="ow">is</span> <span class="bp">None</span>
                        <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">to_field_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">related_fields</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">from_field</span><span class="p">,</span> <span class="n">to_field</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">related_fields</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;_related_fields&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_related_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolve_related_fields</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_related_fields</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reverse_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[(</span><span class="n">rhs_field</span><span class="p">,</span> <span class="n">lhs_field</span><span class="p">)</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">local_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">lhs_field</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">foreign_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">rhs_field</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_local_related_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_foreign_related_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">fields</span><span class="p">):</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="c"># Gotcha: in some cases (like fixture loading) a model can have</span>
            <span class="c"># different values in parent_ptr_id and parent&#39;s id. So, use</span>
            <span class="c"># instance.pk (that is, parent_ptr_id) when asked for instance.id.</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
                <span class="n">possible_parent_link</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_ancestor_link</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">possible_parent_link</span> <span class="ow">or</span>
                        <span class="n">possible_parent_link</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">or</span>
                        <span class="n">possible_parent_link</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">):</span>
                    <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>
                    <span class="k">continue</span>
            <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_attname_column</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">attname</span><span class="p">,</span> <span class="n">column</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">attname</span><span class="p">,</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_joining_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reverse_join</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_related_fields</span> <span class="k">if</span> <span class="n">reverse_join</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">((</span><span class="n">lhs_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span> <span class="n">rhs_field</span><span class="o">.</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="n">source</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_reverse_joining_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_joining_columns</span><span class="p">(</span><span class="n">reverse_join</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_extra_descriptor_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns an extra filter condition for related object fetching when</span>
<span class="sd">        user does &#39;instance.fieldname&#39;, that is the extra filter is used in</span>
<span class="sd">        the descriptor of the field.</span>

<span class="sd">        The filter should be either a dict usable in .filter(**kwargs) call or</span>
<span class="sd">        a Q-object. The condition will be ANDed together with the relation&#39;s</span>
<span class="sd">        joining columns.</span>

<span class="sd">        A parallel method is get_extra_restriction() which is used in</span>
<span class="sd">        JOIN and subquery conditions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">get_extra_restriction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">where_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">related_alias</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a pair condition used for joining and subquery pushdown. The</span>
<span class="sd">        condition is something that responds to as_sql(qn, connection) method.</span>

<span class="sd">        Note that currently referring both the &#39;alias&#39; and &#39;related_alias&#39;</span>
<span class="sd">        will not work in some conditions, like subquery pushdown.</span>

<span class="sd">        A parallel method is get_extra_descriptor_filter() which is used in</span>
<span class="sd">        instance.fieldname related object fetching.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get path from this field to the related model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span><span class="n">from_opts</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get path from the related model to this field&#39;s model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">pathinfos</span> <span class="o">=</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span><span class="n">from_opts</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="p">,),</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">,</span> <span class="bp">False</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">pathinfos</span>

    <span class="k">def</span> <span class="nf">get_lookup_constraint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">constraint_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">lookups</span><span class="p">,</span>
                              <span class="n">raw_value</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.db.models.sql.where</span> <span class="kn">import</span> <span class="n">SubqueryConstraint</span><span class="p">,</span> <span class="n">AND</span><span class="p">,</span> <span class="n">OR</span>
        <span class="n">root_constraint</span> <span class="o">=</span> <span class="n">constraint_class</span><span class="p">()</span>
        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">targets</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">sources</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lookups</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldError</span><span class="p">(</span><span class="s">&#39;Relation fields do not support nested lookups&#39;</span><span class="p">)</span>
        <span class="n">lookup_type</span> <span class="o">=</span> <span class="n">lookups</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">def</span> <span class="nf">get_normalized_value</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Model</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">Model</span><span class="p">):</span>
                <span class="n">value_list</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">sources</span><span class="p">:</span>
                    <span class="c"># Account for one-to-one relations when sent a different model</span>
                    <span class="k">while</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">source</span><span class="o">.</span><span class="n">model</span><span class="p">)</span> <span class="ow">and</span> <span class="n">source</span><span class="o">.</span><span class="n">rel</span><span class="p">:</span>
                        <span class="n">source</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span>
                    <span class="n">value_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">source</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
                <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">value_list</span><span class="p">)</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">(</span><span class="n">value</span><span class="p">,)</span>
            <span class="k">return</span> <span class="n">value</span>

        <span class="n">is_multicolumn</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">raw_value</span><span class="p">,</span> <span class="s">&#39;_as_sql&#39;</span><span class="p">)</span> <span class="ow">or</span>
                <span class="nb">hasattr</span><span class="p">(</span><span class="n">raw_value</span><span class="p">,</span> <span class="s">&#39;get_compiler&#39;</span><span class="p">)):</span>
            <span class="n">root_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">SubqueryConstraint</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="p">[</span><span class="n">target</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">target</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">],</span>
                                                   <span class="p">[</span><span class="n">source</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">source</span> <span class="ow">in</span> <span class="n">sources</span><span class="p">],</span> <span class="n">raw_value</span><span class="p">),</span>
                                <span class="n">AND</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lookup_type</span> <span class="o">==</span> <span class="s">&#39;isnull&#39;</span><span class="p">:</span>
            <span class="n">root_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">IsNull</span><span class="p">(</span><span class="n">Col</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">targets</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">raw_value</span><span class="p">),</span> <span class="n">AND</span><span class="p">)</span>
        <span class="k">elif</span> <span class="p">(</span><span class="n">lookup_type</span> <span class="o">==</span> <span class="s">&#39;exact&#39;</span> <span class="ow">or</span> <span class="p">(</span><span class="n">lookup_type</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;gt&#39;</span><span class="p">,</span> <span class="s">&#39;lt&#39;</span><span class="p">,</span> <span class="s">&#39;gte&#39;</span><span class="p">,</span> <span class="s">&#39;lte&#39;</span><span class="p">]</span>
                                         <span class="ow">and</span> <span class="ow">not</span> <span class="n">is_multicolumn</span><span class="p">)):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">get_normalized_value</span><span class="p">(</span><span class="n">raw_value</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">target</span><span class="p">,</span> <span class="n">source</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">sources</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
                <span class="n">lookup_class</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="n">get_lookup</span><span class="p">(</span><span class="n">lookup_type</span><span class="p">)</span>
                <span class="n">root_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span>
                    <span class="n">lookup_class</span><span class="p">(</span><span class="n">Col</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">source</span><span class="p">),</span> <span class="n">val</span><span class="p">),</span> <span class="n">AND</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lookup_type</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;range&#39;</span><span class="p">,</span> <span class="s">&#39;in&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">is_multicolumn</span><span class="p">:</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">get_normalized_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">raw_value</span><span class="p">]</span>
            <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]</span>
            <span class="n">lookup_class</span> <span class="o">=</span> <span class="n">targets</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get_lookup</span><span class="p">(</span><span class="n">lookup_type</span><span class="p">)</span>
            <span class="n">root_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lookup_class</span><span class="p">(</span><span class="n">Col</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">targets</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sources</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">value</span><span class="p">),</span> <span class="n">AND</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lookup_type</span> <span class="o">==</span> <span class="s">&#39;in&#39;</span><span class="p">:</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">get_normalized_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">raw_value</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">:</span>
                <span class="n">value_constraint</span> <span class="o">=</span> <span class="n">constraint_class</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">source</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">sources</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
                    <span class="n">lookup_class</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="n">get_lookup</span><span class="p">(</span><span class="s">&#39;exact&#39;</span><span class="p">)</span>
                    <span class="n">lookup</span> <span class="o">=</span> <span class="n">lookup_class</span><span class="p">(</span><span class="n">Col</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">source</span><span class="p">),</span> <span class="n">val</span><span class="p">)</span>
                    <span class="n">value_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lookup</span><span class="p">,</span> <span class="n">AND</span><span class="p">)</span>
                <span class="n">root_constraint</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">value_constraint</span><span class="p">,</span> <span class="n">OR</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&#39;Related Field got invalid lookup: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">lookup_type</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">root_constraint</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">attnames</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_defaults</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">virtual_only</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">virtual_only</span><span class="o">=</span><span class="n">virtual_only</span><span class="p">)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">ReverseSingleRelatedObjectDescriptor</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="c"># Internal FK&#39;s - i.e., those with a related name ending with &#39;+&#39; -</span>
        <span class="c"># and swapped models don&#39;t get a related descriptor.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_accessor_class</span><span class="p">(</span><span class="n">related</span><span class="p">))</span>
            <span class="c"># While &#39;limit_choices_to&#39; might be a callable, simply pass</span>
            <span class="c"># it along for later - this is too early because it&#39;s still</span>
            <span class="c"># model load time.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">:</span>
                <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_fkey_lookups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ForeignKey</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">):</span>
    <span class="n">empty_strings_allowed</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">default_error_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;invalid&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%(model)s</span><span class="s"> instance with pk </span><span class="si">%(pk)r</span><span class="s"> does not exist.&#39;</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;Foreign Key (type determined by related field)&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">to_field</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">rel_class</span><span class="o">=</span><span class="n">ManyToOneRel</span><span class="p">,</span>
                 <span class="n">db_constraint</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>  <span class="c"># to._meta doesn&#39;t exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT</span>
            <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">),</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">(</span><span class="si">%r</span><span class="s">) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># For backwards compatibility purposes, we need to *try* and set</span>
            <span class="c"># the to_field during FK construction. It won&#39;t be guaranteed to</span>
            <span class="c"># be correct until contribute_to_class is called. Refs #12190.</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="n">to_field</span> <span class="ow">or</span> <span class="p">(</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="ow">and</span> <span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

        <span class="k">if</span> <span class="s">&#39;db_index&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_index&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span> <span class="o">=</span> <span class="n">db_constraint</span>

        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;rel&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">rel_class</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">to_field</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">related_query_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_query_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;limit_choices_to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">parent_link</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;parent_link&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">),</span>
            <span class="n">on_delete</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;on_delete&#39;</span><span class="p">,</span> <span class="n">CASCADE</span><span class="p">),</span>
        <span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;self&#39;</span><span class="p">],</span> <span class="p">[</span><span class="n">to_field</span><span class="p">],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_on_delete</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_on_delete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">on_delete</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;on_delete&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">on_delete</span> <span class="o">==</span> <span class="n">SET_NULL</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s">&#39;Field specifies on_delete=SET_NULL, but cannot be null.&#39;</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s">&#39;Set null=True argument on the field, or change the on_delete rule.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E320&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="n">on_delete</span> <span class="o">==</span> <span class="n">SET_DEFAULT</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s">&#39;Field specifies on_delete=SET_DEFAULT, but has no default value.&#39;</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s">&#39;Set a default value, or change the on_delete rule.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E321&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to_fields&#39;</span><span class="p">]</span>
        <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;from_fields&#39;</span><span class="p">]</span>
        <span class="c"># Handle the simpler arguments</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_index</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_index&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_index&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">True</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_constraint&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span>
        <span class="c"># Rel needs more work.</span>
        <span class="n">to_meta</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="s">&quot;_meta&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">to_meta</span> <span class="ow">or</span> <span class="p">(</span><span class="n">to_meta</span><span class="o">.</span><span class="n">pk</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span> <span class="o">!=</span> <span class="n">to_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to_field&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">related_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get path from the related model to this field&#39;s model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">pathinfos</span> <span class="o">=</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span><span class="n">from_opts</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="p">,),</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">,</span> <span class="bp">False</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">pathinfos</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">model_instance</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">model_instance</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">using</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_read</span><span class="p">(</span><span class="n">model_instance</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">model_instance</span><span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
            <span class="o">**</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span><span class="p">:</span> <span class="n">value</span><span class="p">}</span>
        <span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">complex_filter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_limit_choices_to</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">qs</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s">&#39;invalid&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;model&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span> <span class="s">&#39;pk&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">},</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_attname</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">_id&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="nf">get_attname_column</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">attname</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_attname</span><span class="p">()</span>
        <span class="n">column</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_column</span> <span class="ow">or</span> <span class="n">attname</span>
        <span class="k">return</span> <span class="n">attname</span><span class="p">,</span> <span class="n">column</span>

    <span class="k">def</span> <span class="nf">get_validator_unique_lookup_type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">__</span><span class="si">%s</span><span class="s">__exact&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_default</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Here we check if the default value is an object and return the to_field if so.&quot;</span>
        <span class="n">field_default</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field_default</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field_default</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">field_default</span>

    <span class="k">def</span> <span class="nf">get_db_prep_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="p">(</span><span class="n">value</span> <span class="o">==</span> <span class="s">&#39;&#39;</span> <span class="ow">and</span>
                             <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">empty_strings_allowed</span> <span class="ow">or</span>
                              <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">get_db_prep_save</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_to_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">obj</span><span class="p">:</span>
            <span class="c"># In required many-to-one fields with only one available choice,</span>
            <span class="c"># select that one available choice. Note: For SelectFields</span>
            <span class="c"># we have to check that the length of choices is *2*, not 1,</span>
            <span class="c"># because SelectFields always have an initial &quot;blank&quot; value.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">blank</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">:</span>
                <span class="n">choice_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_choices_default</span><span class="p">()</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">choice_list</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">smart_text</span><span class="p">(</span><span class="n">choice_list</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">value_to_string</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">contribute_to_related_class</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">db</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;using&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot create form field for </span><span class="si">%r</span><span class="s"> yet, because &quot;</span>
                             <span class="s">&quot;its related model </span><span class="si">%r</span><span class="s"> has not been loaded yet&quot;</span> <span class="o">%</span>
                             <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">))</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">,</span>
            <span class="s">&#39;queryset&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">),</span>
            <span class="s">&#39;to_field_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="c"># The database column type of a ForeignKey is the column type</span>
        <span class="c"># of the field to which it points. An exception is if the ForeignKey</span>
        <span class="c"># points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,</span>
        <span class="c"># in which case the column type is simply that of an IntegerField.</span>
        <span class="c"># If the database needs similar types for key fields however, the only</span>
        <span class="c"># thing we can do is making AutoField an IntegerField.</span>
        <span class="n">rel_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_field</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">rel_field</span><span class="p">,</span> <span class="n">AutoField</span><span class="p">)</span> <span class="ow">or</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">related_fields_match_type</span> <span class="ow">and</span>
                <span class="nb">isinstance</span><span class="p">(</span><span class="n">rel_field</span><span class="p">,</span> <span class="p">(</span><span class="n">PositiveIntegerField</span><span class="p">,</span>
                                       <span class="n">PositiveSmallIntegerField</span><span class="p">)))):</span>
            <span class="k">return</span> <span class="n">IntegerField</span><span class="p">()</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">db_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="p">),</span> <span class="s">&quot;check&quot;</span><span class="p">:</span> <span class="p">[]}</span>


<span class="k">class</span> <span class="nc">OneToOneField</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A OneToOneField is essentially the same as a ForeignKey, with the exception</span>
<span class="sd">    that always carries a &quot;unique&quot; constraint with it and the reverse relation</span>
<span class="sd">    always returns the object pointed to (since there will only ever be one),</span>
<span class="sd">    rather than returning a list.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">related_accessor_class</span> <span class="o">=</span> <span class="n">SingleRelatedObjectDescriptor</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;One-to-one relationship&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">to_field</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;unique&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">OneToOneField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">to_field</span><span class="p">,</span> <span class="n">OneToOneRel</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">OneToOneField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="k">if</span> <span class="s">&quot;unique&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;unique&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">OneToOneField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_form_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">):</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">create_many_to_many_intermediary_model</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">klass</span><span class="p">):</span>
    <span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
    <span class="n">managed</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">!=</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">:</span>
        <span class="n">to_model</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
        <span class="n">to</span> <span class="o">=</span> <span class="n">to_model</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="k">def</span> <span class="nf">set_managed</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">cls</span><span class="p">):</span>
            <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="ow">or</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span>
        <span class="n">add_lazy_relation</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">to_model</span><span class="p">,</span> <span class="n">set_managed</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
        <span class="n">to</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
        <span class="n">to_model</span> <span class="o">=</span> <span class="n">klass</span>
        <span class="n">managed</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">to</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
        <span class="n">to_model</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
        <span class="n">managed</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="ow">or</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span> <span class="ow">or</span> <span class="n">to</span> <span class="o">==</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">:</span>
        <span class="n">from_</span> <span class="o">=</span> <span class="s">&#39;from_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">to</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="n">to</span> <span class="o">=</span> <span class="s">&#39;to_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">to</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">from_</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
        <span class="n">to</span> <span class="o">=</span> <span class="n">to</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">meta</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="s">&#39;Meta&#39;</span><span class="p">),</span> <span class="p">(</span><span class="nb">object</span><span class="p">,),</span> <span class="p">{</span>
        <span class="s">&#39;db_table&#39;</span><span class="p">:</span> <span class="n">field</span><span class="o">.</span><span class="n">_get_m2m_db_table</span><span class="p">(</span><span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="p">),</span>
        <span class="s">&#39;managed&#39;</span><span class="p">:</span> <span class="n">managed</span><span class="p">,</span>
        <span class="s">&#39;auto_created&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="p">,</span>
        <span class="s">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
        <span class="s">&#39;db_tablespace&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span>
        <span class="s">&#39;unique_together&#39;</span><span class="p">:</span> <span class="p">(</span><span class="n">from_</span><span class="p">,</span> <span class="n">to</span><span class="p">),</span>
        <span class="s">&#39;verbose_name&#39;</span><span class="p">:</span> <span class="s">&#39;</span><span class="si">%(from)s</span><span class="s">-</span><span class="si">%(to)s</span><span class="s"> relationship&#39;</span> <span class="o">%</span> <span class="p">{</span><span class="s">&#39;from&#39;</span><span class="p">:</span> <span class="n">from_</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">:</span> <span class="n">to</span><span class="p">},</span>
        <span class="s">&#39;verbose_name_plural&#39;</span><span class="p">:</span> <span class="s">&#39;</span><span class="si">%(from)s</span><span class="s">-</span><span class="si">%(to)s</span><span class="s"> relationships&#39;</span> <span class="o">%</span> <span class="p">{</span><span class="s">&#39;from&#39;</span><span class="p">:</span> <span class="n">from_</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">:</span> <span class="n">to</span><span class="p">},</span>
        <span class="s">&#39;apps&#39;</span><span class="p">:</span> <span class="n">field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="p">,</span>
    <span class="p">})</span>
    <span class="c"># Construct and return the new class.</span>
    <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">,),</span> <span class="p">{</span>
        <span class="s">&#39;Meta&#39;</span><span class="p">:</span> <span class="n">meta</span><span class="p">,</span>
        <span class="s">&#39;__module__&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="n">__module__</span><span class="p">,</span>
        <span class="n">from_</span><span class="p">:</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">+&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span> <span class="n">db_constraint</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">),</span>
        <span class="n">to</span><span class="p">:</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">to_model</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">+&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span> <span class="n">db_constraint</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">)</span>
    <span class="p">})</span>


<span class="k">class</span> <span class="nc">ManyToManyField</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">):</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;Many-to-many relationship&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">db_constraint</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">swappable</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>  <span class="c"># to._meta doesn&#39;t exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT</span>
            <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">),</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">(</span><span class="si">%r</span><span class="s">) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">)</span>
            <span class="c"># Class names must be ASCII in Python 2.x, so we forcibly coerce it here to break early if there&#39;s a problem.</span>
            <span class="n">to</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">to</span><span class="p">)</span>

        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;verbose_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;verbose_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;rel&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ManyToManyRel</span><span class="p">(</span><span class="n">to</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">related_query_name</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;related_query_name&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;limit_choices_to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">symmetrical</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;symmetrical&#39;</span><span class="p">,</span> <span class="n">to</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">),</span>
            <span class="n">through</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;through&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">through_fields</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;through_fields&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
            <span class="n">db_constraint</span><span class="o">=</span><span class="n">db_constraint</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span> <span class="o">=</span> <span class="n">swappable</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;db_table&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;rel&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">through</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">,</span> <span class="s">&quot;Cannot specify a db_table if an intermediary model is used.&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ManyToManyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyToManyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_unique</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_relationship_model</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s">&#39;ManyToManyFields cannot be unique.&#39;</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E330&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_relationship_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="s">&#39;_meta&#39;</span><span class="p">):</span>
            <span class="n">qualified_model_name</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">qualified_model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
            <span class="c"># The relationship model is not installed.</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="p">(</span><span class="s">&quot;Field specifies a many-to-many relation through model &quot;</span>
                     <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;, which has not been installed.&quot;</span><span class="p">)</span> <span class="o">%</span>
                    <span class="n">qualified_model_name</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E331&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>

            <span class="k">assert</span> <span class="n">from_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">,</span> \
                <span class="s">&quot;ManyToManyField with intermediate &quot;</span> \
                <span class="s">&quot;tables cannot be checked if you don&#39;t pass the model &quot;</span> \
                <span class="s">&quot;where the field is attached to.&quot;</span>

            <span class="c"># Set some useful local variables</span>
            <span class="n">to_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
            <span class="n">from_model_name</span> <span class="o">=</span> <span class="n">from_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to_model</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="n">to_model_name</span> <span class="o">=</span> <span class="n">to_model</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">to_model_name</span> <span class="o">=</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="n">relationship_model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="n">self_referential</span> <span class="o">=</span> <span class="n">from_model</span> <span class="o">==</span> <span class="n">to_model</span>

            <span class="c"># Check symmetrical attribute.</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">self_referential</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">symmetrical</span> <span class="ow">and</span>
                    <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s">&#39;Many-to-many fields with intermediate tables must not be symmetrical.&#39;</span><span class="p">,</span>
                        <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E332&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="c"># Count foreign keys in intermediate model</span>
            <span class="k">if</span> <span class="n">self_referential</span><span class="p">:</span>
                <span class="n">seen_self</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">from_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_self</span> <span class="o">&gt;</span> <span class="mi">2</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="p">(</span><span class="s">&quot;The model is used as an intermediate model by &quot;</span>
                             <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;, but it has more than two foreign keys &quot;</span>
                             <span class="s">&quot;to &#39;</span><span class="si">%s</span><span class="s">&#39;, which is ambiguous. You must specify &quot;</span>
                             <span class="s">&quot;which two foreign keys Django should use via the &quot;</span>
                             <span class="s">&quot;through_fields keyword argument.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Use through_fields to specify which two &quot;</span>
                                  <span class="s">&quot;foreign keys Django should use.&quot;</span><span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E333&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="c"># Count foreign keys in relationship model</span>
                <span class="n">seen_from</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">from_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>
                <span class="n">seen_to</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">to_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_from</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="p">(</span><span class="s">&quot;The model is used as an intermediate model by &quot;</span>
                             <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;, but it has more than one foreign key &quot;</span>
                             <span class="s">&quot;from &#39;</span><span class="si">%s</span><span class="s">&#39;, which is ambiguous. You must specify &quot;</span>
                             <span class="s">&quot;which foreign key Django should use via the &quot;</span>
                             <span class="s">&quot;through_fields keyword argument.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;If you want to create a recursive relationship, &#39;</span>
                                  <span class="s">&#39;use ForeignKey(&quot;self&quot;, symmetrical=False, &#39;</span>
                                  <span class="s">&#39;through=&quot;</span><span class="si">%s</span><span class="s">&quot;).&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">relationship_model_name</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E334&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_to</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="p">(</span><span class="s">&quot;The model is used as an intermediate model by &quot;</span>
                             <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;, but it has more than one foreign key &quot;</span>
                             <span class="s">&quot;to &#39;</span><span class="si">%s</span><span class="s">&#39;, which is ambiguous. You must specify &quot;</span>
                             <span class="s">&quot;which foreign key Django should use via the &quot;</span>
                             <span class="s">&quot;through_fields keyword argument.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;If you want to create a recursive &#39;</span>
                                  <span class="s">&#39;relationship, use ForeignKey(&quot;self&quot;, &#39;</span>
                                  <span class="s">&#39;symmetrical=False, through=&quot;</span><span class="si">%s</span><span class="s">&quot;).&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">relationship_model_name</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E335&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_from</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">seen_to</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="p">(</span><span class="s">&quot;The model is used as an intermediate model by &quot;</span>
                             <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;, but it does not have a foreign key to &#39;</span><span class="si">%s</span><span class="s">&#39; or &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">,</span> <span class="n">to_model_name</span>
                            <span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E336&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

        <span class="c"># Validate `through_fields`</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># Validate that we&#39;re given an iterable of at least two items</span>
            <span class="c"># and that none of them is &quot;falsy&quot;</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">2</span> <span class="ow">and</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="p">(</span><span class="s">&quot;Field specifies &#39;through_fields&#39; but does not &quot;</span>
                         <span class="s">&quot;provide the names of the two link fields that should be &quot;</span>
                         <span class="s">&quot;used for the relation through model &quot;</span>
                         <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="n">qualified_model_name</span><span class="p">,</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;Make sure you specify &#39;through_fields&#39; as &quot;</span>
                              <span class="s">&quot;through_fields=(&#39;field1&#39;, &#39;field2&#39;)&quot;</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E337&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="c"># Validate the given through fields -- they should be actual</span>
            <span class="c"># fields on the through model, and also be foreign keys to the</span>
            <span class="c"># expected models</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">assert</span> <span class="n">from_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">,</span> \
                    <span class="s">&quot;ManyToManyField with intermediate &quot;</span> \
                    <span class="s">&quot;tables cannot be checked if you don&#39;t pass the model &quot;</span> \
                    <span class="s">&quot;where the field is attached to.&quot;</span>

                <span class="n">source</span><span class="p">,</span> <span class="n">through</span><span class="p">,</span> <span class="n">target</span> <span class="o">=</span> <span class="n">from_model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
                <span class="n">source_field_name</span><span class="p">,</span> <span class="n">target_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>

                <span class="k">for</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">related_model</span> <span class="ow">in</span> <span class="p">((</span><span class="n">source_field_name</span><span class="p">,</span> <span class="n">source</span><span class="p">),</span>
                                                  <span class="p">(</span><span class="n">target_field_name</span><span class="p">,</span> <span class="n">target</span><span class="p">)):</span>

                    <span class="n">possible_field_names</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="o">==</span> <span class="n">related_model</span><span class="p">:</span>
                            <span class="n">possible_field_names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">possible_field_names</span><span class="p">:</span>
                        <span class="n">hint</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;Did you mean one of the following foreign &quot;</span>
                                <span class="s">&quot;keys to &#39;</span><span class="si">%s</span><span class="s">&#39;: </span><span class="si">%s</span><span class="s">?&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                                                        <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">possible_field_names</span><span class="p">))</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">hint</span> <span class="o">=</span> <span class="bp">None</span>

                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">field</span> <span class="o">=</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
                        <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                                <span class="p">(</span><span class="s">&quot;The intermediary model &#39;</span><span class="si">%s</span><span class="s">&#39; has no field &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span>
                                    <span class="n">qualified_model_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                                <span class="n">hint</span><span class="o">=</span><span class="n">hint</span><span class="p">,</span>
                                <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                                <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E338&#39;</span><span class="p">,</span>
                            <span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span> <span class="ow">and</span>
                                <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="o">==</span> <span class="n">related_model</span><span class="p">):</span>
                            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                                    <span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39; is not a foreign key to &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                        <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span>
                                        <span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">),</span>
                                    <span class="n">hint</span><span class="o">=</span><span class="n">hint</span><span class="p">,</span>
                                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                                    <span class="nb">id</span><span class="o">=</span><span class="s">&#39;fields.E339&#39;</span><span class="p">,</span>
                                <span class="p">)</span>
                            <span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyToManyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="c"># Handle the simpler arguments</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_table&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">db_constraint</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">True</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;db_constraint&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">db_constraint</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;related_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_query_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;related_query_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_query_name</span>
        <span class="c"># Rel needs more work.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="p">,</span> <span class="s">&#39;through&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;through&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;through&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>
        <span class="c"># If swappable is True, then see if we&#39;re actually pointing to the target</span>
        <span class="c"># of a swap.</span>
        <span class="n">swappable_setting</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable_setting</span>
        <span class="k">if</span> <span class="n">swappable_setting</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># If it&#39;s already a settings reference, error</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">],</span> <span class="s">&quot;setting_name&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span> <span class="o">!=</span> <span class="n">swappable_setting</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot deconstruct a ManyToManyField pointing to a model that is swapped in place of more than one model (</span><span class="si">%s</span><span class="s"> and </span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span><span class="p">,</span> <span class="n">swappable_setting</span><span class="p">))</span>
            <span class="c"># Set it</span>
            <span class="kn">from</span> <span class="nn">django.db.migrations.writer</span> <span class="kn">import</span> <span class="n">SettingsReference</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">SettingsReference</span><span class="p">(</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;to&#39;</span><span class="p">],</span>
                <span class="n">swappable_setting</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">_get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">direct</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Called by both direct and indirect m2m traversal.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pathinfos</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">int_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span>
        <span class="n">linkfield1</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">linkfield2</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">direct</span><span class="p">:</span>
            <span class="n">join1infos</span> <span class="o">=</span> <span class="n">linkfield1</span><span class="o">.</span><span class="n">get_reverse_path_info</span><span class="p">()</span>
            <span class="n">join2infos</span> <span class="o">=</span> <span class="n">linkfield2</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">join1infos</span> <span class="o">=</span> <span class="n">linkfield2</span><span class="o">.</span><span class="n">get_reverse_path_info</span><span class="p">()</span>
            <span class="n">join2infos</span> <span class="o">=</span> <span class="n">linkfield1</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()</span>
        <span class="n">pathinfos</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">join1infos</span><span class="p">)</span>
        <span class="n">pathinfos</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">join2infos</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pathinfos</span>

    <span class="k">def</span> <span class="nf">get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_path_info</span><span class="p">(</span><span class="n">direct</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_path_info</span><span class="p">(</span><span class="n">direct</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_choices_default</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Field</span><span class="o">.</span><span class="n">get_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_blank</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_m2m_db_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">opts</span><span class="p">):</span>
        <span class="s">&quot;Function that can be curried to provide the m2m table name for this relation&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">utils</span><span class="o">.</span><span class="n">truncate_name</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">),</span>
                                      <span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">_get_m2m_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
        <span class="s">&quot;Function that can be curried to provide the source accessor or DB column name for the m2m table&quot;</span>
        <span class="n">cache_attr</span> <span class="o">=</span> <span class="s">&#39;_m2m_</span><span class="si">%s</span><span class="s">_cache&#39;</span> <span class="o">%</span> <span class="n">attr</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span> <span class="ow">and</span> \
                    <span class="p">(</span><span class="n">link_field_name</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">link_field_name</span> <span class="o">==</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_m2m_reverse_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
        <span class="s">&quot;Function that can be curried to provide the related accessor or DB column name for the m2m table&quot;</span>
        <span class="n">cache_attr</span> <span class="o">=</span> <span class="s">&#39;_m2m_reverse_</span><span class="si">%s</span><span class="s">_cache&#39;</span> <span class="o">%</span> <span class="n">attr</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>
        <span class="n">found</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">parent_model</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">link_field_name</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">parent_model</span><span class="p">:</span>
                    <span class="c"># If this is an m2m-intermediate to self,</span>
                    <span class="c"># the first foreign key you find will be</span>
                    <span class="c"># the source column. Keep searching for</span>
                    <span class="c"># the second foreign key.</span>
                    <span class="k">if</span> <span class="n">found</span><span class="p">:</span>
                        <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                        <span class="k">break</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">found</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="k">elif</span> <span class="n">link_field_name</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">link_field_name</span> <span class="o">==</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_to_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">obj</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
            <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">instance</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span> <span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">qs</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># In required many-to-many fields with only one available choice,</span>
            <span class="c"># select that one available choice.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">blank</span><span class="p">:</span>
                <span class="n">choices_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_choices_default</span><span class="p">()</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">choices_list</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">choices_list</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
        <span class="k">return</span> <span class="n">smart_text</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="c"># To support multiple relations to self, it&#39;s useful to have a non-None</span>
        <span class="c"># related name on symmetrical relations for internal reasons. The</span>
        <span class="c"># concept doesn&#39;t make a lot of sense externally (&quot;you want me to</span>
        <span class="c"># specify *what* on my non-reversible relation?!&quot;), so we set it up</span>
        <span class="c"># automatically. The funky name reduces the chance of an accidental</span>
        <span class="c"># clash.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">symmetrical</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">==</span> <span class="s">&quot;self&quot;</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span> <span class="o">==</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">_rel_+&quot;</span> <span class="o">%</span> <span class="n">name</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ManyToManyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

        <span class="c"># The intermediate m2m model is not auto created if:</span>
        <span class="c">#  1) There is a manually specified intermediate, or</span>
        <span class="c">#  2) The class owning the m2m field is abstract.</span>
        <span class="c">#  3) The class owning the m2m field has been swapped out.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">create_many_to_many_intermediary_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>

        <span class="c"># Add the descriptor for the m2m relation</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">ReverseManyRelatedObjectsDescriptor</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

        <span class="c"># Set up the accessor for the m2m table name for the relation</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_db_table</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_db_table</span><span class="p">,</span> <span class="n">cls</span><span class="o">.</span><span class="n">_meta</span><span class="p">)</span>

        <span class="c"># Populate some necessary rel arguments so that cross-app relations</span>
        <span class="c"># work correctly.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">def</span> <span class="nf">resolve_through_model</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">cls</span><span class="p">):</span>
                <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">model</span>
            <span class="n">add_lazy_relation</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">resolve_through_model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="c"># Internal M2Ms (i.e., those with a related name ending with &#39;+&#39;)</span>
        <span class="c"># and swapped models don&#39;t get a related descriptor.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span> <span class="n">ManyRelatedObjectsDescriptor</span><span class="p">(</span><span class="n">related</span><span class="p">))</span>

        <span class="c"># Set up the accessors for the column names on the m2m table</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_column_name</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;column&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_name</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;column&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_field_name</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;name&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;name&#39;</span><span class="p">)</span>

        <span class="n">get_m2m_rel</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_target_field_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">get_m2m_rel</span><span class="p">()</span><span class="o">.</span><span class="n">field_name</span>
        <span class="n">get_m2m_reverse_rel</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s">&#39;rel&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_target_field_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">get_m2m_reverse_rel</span><span class="p">()</span><span class="o">.</span><span class="n">field_name</span>

    <span class="k">def</span> <span class="nf">set_attributes_from_rel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">value_from_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="s">&quot;Returns the value of this field in the given model instance.&quot;</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">save_form_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">db</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;using&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelMultipleChoiceField</span><span class="p">,</span>
            <span class="s">&#39;queryset&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="c"># If initial is passed in, it&#39;s a list of related objects, but the</span>
        <span class="c"># MultipleChoiceField takes a list of IDs.</span>
        <span class="k">if</span> <span class="n">defaults</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;initial&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">initial</span> <span class="o">=</span> <span class="n">defaults</span><span class="p">[</span><span class="s">&#39;initial&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">initial</span><span class="p">):</span>
                <span class="n">initial</span> <span class="o">=</span> <span class="n">initial</span><span class="p">()</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s">&#39;initial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">initial</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ManyToManyField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="c"># A ManyToManyField is not represented by a single column,</span>
        <span class="c"># so return None.</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">db_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span> <span class="s">&quot;check&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">}</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../../index.html">Module code</a>
        
          <ul><li><a href="../../../../django.html">django</a>
        
          <ul><li><a href="../../../db.html">django.db</a>
        
          <ul><li><a href="../../models.html">django.db.models</a>
        
        <ul><li>django.db.models.fields.related</li></ul>
        </li></ul></li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>